package com.mystnihon.widget.settings;

import android.content.Context;
import android.preference.EditTextPreference;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

import com.mystnihon.library.R;

public class PasswordEditTextPreference extends EditTextPreference {
	EditText mEditText;

	public PasswordEditTextPreference(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public PasswordEditTextPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public PasswordEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onBindDialogView(View view) {
		super.onBindDialogView(view);
		CheckBox checkBox = (CheckBox) view.findViewById(R.id.prefs_show_password);
		mEditText = (EditText) view.findViewById(android.R.id.edit);
		mEditText.setText(getText());
		checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					mEditText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
				} else {
					mEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
				}

			}
		});

	}

	@Override
	protected void onDialogClosed(boolean positiveResult) {
		super.onDialogClosed(positiveResult);

		if (positiveResult) {
			String value = mEditText.getText().toString();
			if (callChangeListener(value)) {
				setText(value);
			}
		}
	}
}
